﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using DAL;
using DataProj.Areas.BackSys.ServiceTools;
using Newtonsoft.Json;

namespace DataProj.Areas.BackSys.Controllers
{
    public class MessageController : BaseController
    {
        // GET: BackSys/Message
        public static MessageImpl _impl;
        public MessageController()
        {
            if (_impl == null) _impl = new MessageImpl();
            if (string.IsNullOrEmpty(MySession.GetSession("messageRows")))
            {
                MySession.WriteSession("messageRows", 10);
            }
            if (string.IsNullOrEmpty(MySession.GetSession("messagePage")))
            {
                MySession.WriteSession("messagePage", 0);
            }
        }
        public ActionResult MessageManage()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Delete(int id = 0)
        {

            if (_impl.Delete(id) == true)
            {
                return Json(1);
            }
            return Json(0);
        }


        [HttpPost]
        public ActionResult ChangePage()
        {
            IDictionary<string, string> set = new Dictionary<string, string>();
            int page = Convert.ToInt16(Request["pageNum"].Trim());
            int totalPages = 0;
            IList<Message> data = new List<Message>();
            switch (LoginType)
            {
                case  "用户":
                    ( totalPages,  data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("messageRows")), page);
                    //MySession.WriteSession("managerTotalPages", totalPages);
                    //return Json(JsonConvert.SerializeObject(data));
                    set.Add("totalPages", totalPages.ToString());
                    set.Add("data", JsonConvert.SerializeObject(data));
                    return Json(JsonConvert.SerializeObject(set));
                case "管理员":
                case "快递员":
                    ( totalPages,  data) = _impl.GetList(Convert.ToInt16(MySession.GetSession("messageRows")), page);
                    //MySession.WriteSession("managerTotalPages", totalPages);
                    //return Json(JsonConvert.SerializeObject(data));
                    set.Add("totalPages", totalPages.ToString());
                    set.Add("data", JsonConvert.SerializeObject(data));
                    return Json(JsonConvert.SerializeObject(set));
                    
                default:
                    break;
            }
            return Json(JsonConvert.SerializeObject(set));
        
        }

    }
}